package com.team06.util;

import com.team06.pojo.Dfdl;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DfdlXmlParserUtil {
    private static final Logger LOGGER = Logger.getLogger(DfdlXmlParserUtil.class.getName());

    public static Dfdl parseXml(String xmlPath) {
        Dfdl dfdl = new Dfdl();
        try {
            File file = new File(xmlPath);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(file);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element metaElement = root.getChild("META");
            if (metaElement != null) {
                dfdl.setMetaSnDR(getChildTextSafe(metaElement, "SNDR"));
                dfdl.setMetaRCVR(getChildTextSafe(metaElement, "RCVR"));
                String seqnText = getChildTextSafe(metaElement, "SEQN");
                if (seqnText != null) {
                    dfdl.setMetaSEQN(Integer.parseInt(seqnText));
                }
                dfdl.setMetaDDTM(getChildTextSafe(metaElement, "DDTM"));
                dfdl.setMetaTYPE(getChildTextSafe(metaElement, "TYPE"));
                dfdl.setMetaSTYP(getChildTextSafe(metaElement, "STYP"));
            }

            // 解析 RECD 节点
            dfdl.setRecd(getChildTextSafe(root, "RECD"));

            // 解析 DFLT 节点列表
            List<Element> dfltElements = root.getChildren("DFLT");
            List<Dfdl.Flight> flights = new ArrayList<>();

            for (Element dfltElement : dfltElements) {
                Dfdl.Flight flight = new Dfdl.Flight();

                // 解析航班基本信息
                flight.setFlid(getChildTextSafe(dfltElement, "FLID"));
                flight.setAfid(getChildTextSafe(dfltElement, "AFID"));
                flight.setFfid(getChildTextSafe(dfltElement, "FFID"));
                flight.setFide(getChildTextSafe(dfltElement, "FIDE"));
                flight.setTest(getChildTextSafe(dfltElement, "TEST"));
                flight.setAwcd(getChildTextSafe(dfltElement, "AWCD"));
                flight.setFlno(getChildTextSafe(dfltElement, "FLNO"));
                flight.setFexd(getChildTextSafe(dfltElement, "FEXD"));
                flight.setFimd(getChildTextSafe(dfltElement, "FIMD"));
                flight.setFlio(getChildTextSafe(dfltElement, "FLIO"));
                flight.setFltk(getChildTextSafe(dfltElement, "FLTK"));
                flight.setFatt(getChildTextSafe(dfltElement, "FATT"));
                flight.setPatt(getChildTextSafe(dfltElement, "PATT"));
                flight.setMfid(getChildTextSafe(dfltElement, "MFID"));
                flight.setMffi(getChildTextSafe(dfltElement, "MFFI"));
                flight.setCftp(getChildTextSafe(dfltElement, "CFTP"));
                flight.setCfno(getChildTextSafe(dfltElement, "CFNO"));
                flight.setStat(getChildTextSafe(dfltElement, "STAT"));
                flight.setAbst(getChildTextSafe(dfltElement, "ABST"));
                flight.setAbrs(getChildTextSafe(dfltElement, "ABRS"));
                flight.setIsta(getChildTextSafe(dfltElement, "ISTA"));
                flight.setIast(getChildTextSafe(dfltElement, "IAST"));
                flight.setIars(getChildTextSafe(dfltElement, "IARS"));
                flight.setMsta(getChildTextSafe(dfltElement, "MSTA"));
                flight.setMabs(getChildTextSafe(dfltElement, "MABS"));
                flight.setMabr(getChildTextSafe(dfltElement, "MABR"));
                flight.setMist(getChildTextSafe(dfltElement, "MIST"));
                flight.setMias(getChildTextSafe(dfltElement, "MIAS"));
                flight.setMiar(getChildTextSafe(dfltElement, "MIAR"));
                flight.setBort(getChildTextSafe(dfltElement, "BORT"));
                flight.setMbor(getChildTextSafe(dfltElement, "MBOR"));
                flight.setTbrt(getChildTextSafe(dfltElement, "TBRT"));
                flight.setMtbr(getChildTextSafe(dfltElement, "MTBR"));
                flight.setLbdt(getChildTextSafe(dfltElement, "LBDT"));
                flight.setMlbd(getChildTextSafe(dfltElement, "MLBD"));
                flight.setPokt(getChildTextSafe(dfltElement, "POKT"));
                flight.setMpok(getChildTextSafe(dfltElement, "MPOK"));
                flight.setApot(getChildTextSafe(dfltElement, "APOT"));
                flight.setDett(getChildTextSafe(dfltElement, "DETT"));
                flight.setDrtt(getChildTextSafe(dfltElement, "DRTT"));
                flight.setDelt(getChildTextSafe(dfltElement, "DELT"));
                flight.setDrlt(getChildTextSafe(dfltElement, "DRLT"));
                flight.setVip(getChildTextSafe(dfltElement, "VIP"));
                flight.setSflg(getChildTextSafe(dfltElement, "SFLG"));
                flight.setPast(getChildTextSafe(dfltElement, "PAST"));
                flight.setFldt(getChildTextSafe(dfltElement, "FLDT"));
                flight.setLldt(getChildTextSafe(dfltElement, "LLDT"));
                flight.setCont(getChildTextSafe(dfltElement, "CONT"));
                flight.setProx(getChildTextSafe(dfltElement, "PROX"));

                // 解析航站楼信息
                Element tmcdElement = dfltElement.getChild("TMCD");
                if (tmcdElement != null) {
                    flight.setNmcd(getChildTextSafe(tmcdElement, "NMCD"));
                    flight.setJmcd(getChildTextSafe(tmcdElement, "JMCD"));
                }

                // 解析机场列表
                List<Dfdl.Airport> airports = new ArrayList<>();
                Element airlElement = dfltElement.getChild("AIRL");
                if (airlElement != null) {
                    List<Element> arptElements = airlElement.getChildren("ARPT");
                    for (Element arptElement : arptElements) {
                        Dfdl.Airport airport = new Dfdl.Airport();
                        airport.setApno(getChildTextSafe(arptElement, "APNO"));
                        airport.setApcd(getChildTextSafe(arptElement, "APCD"));
                        airport.setFptt(getChildTextSafe(arptElement, "FPTT"));
                        airport.setFett(getChildTextSafe(arptElement, "FETT"));
                        airport.setFrtt(getChildTextSafe(arptElement, "FRTT"));
                        airport.setFplt(getChildTextSafe(arptElement, "FPLT"));
                        airport.setFelt(getChildTextSafe(arptElement, "FELT"));
                        airport.setFrlt(getChildTextSafe(arptElement, "FRLT"));
                        airport.setApat(getChildTextSafe(arptElement, "APAT"));
                        airports.add(airport);
                    }
                }
                flight.setAirports(airports);

                // 解析行李列表
                List<Dfdl.Belt> belts = new ArrayList<>();
                Element bllsElement = dfltElement.getChild("BLLS");
                if (bllsElement != null) {
                    List<Element> beltElements = bllsElement.getChildren("BELT");
                    for (Element beltElement : beltElements) {
                        Dfdl.Belt belt = new Dfdl.Belt();
                        belt.setBtno(getChildTextSafe(beltElement, "BTNO"));
                        belt.setId(getChildTextSafe(beltElement, "ID"));
                        belt.setCode(getChildTextSafe(beltElement, "CODE"));
                        belt.setBtat(getChildTextSafe(beltElement, "BTAT"));
                        belt.setEstr(getChildTextSafe(beltElement, "ESTR"));
                        belt.setEend(getChildTextSafe(beltElement, "EEND"));
                        belt.setRstr(getChildTextSafe(beltElement, "RSTR"));
                        belt.setRend(getChildTextSafe(beltElement, "REND"));
                        belt.setBtsc(getChildTextSafe(beltElement, "BTSC"));
                        belts.add(belt);
                    }
                }
                flight.setBelts(belts);

                flights.add(flight);
            }

            dfdl.setFlights(flights);

        } catch (JDOMException | IOException | NumberFormatException e) {
            LOGGER.log(Level.SEVERE, "Failed to parse XML file: " + xmlPath, e);
            return null;
        }
        return dfdl;
    }

    private static String getChildTextSafe(Element parent, String childName) {
        Element child = parent.getChild(childName);
        return child != null ? child.getText() : null;
    }
}